﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web;

/// <summary>
///TCommentClass 的摘要说明
/// </summary>
public class TComment
{
    private static readonly string _connectionString;

    private int m_iID;

    public int IID
    {
        get { return m_iID; }
    }
    private int m_iUserID;

    public int IUserID
    {
        get { return m_iUserID; }
    }
    private int m_iTheaterID;

    public int ITheaterID
    {
        get { return m_iTheaterID; }
    }
    private DateTime m_dtDate;

    public DateTime DtDate
    {
        get { return m_dtDate; }
    }
    private string m_sTitle;

    public string STitle
    {
        get { return m_sTitle; }
    }
    private string m_sDetail;

    public string SDetail
    {
        get { return m_sDetail; }
    }

    public TComment(int id, int userid, int theaterid
        , DateTime date, string title, string detail)
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
        m_iID = id;
        m_iUserID = userid;
        m_iTheaterID = theaterid;
        m_dtDate = date;
        m_sDetail = detail;
        m_sTitle = title;
	}

    //1)

    /// <summary>
    /// 添加一条评论到数据库		静态
    /// </summary>
    /// <param name="userid">int userid </param>
    /// <param name="theaterid">int theaterid</param>
    /// <param name="title">string title</param>
    /// <param name="detail">string detail</param>
    public static bool AddT_Comment(int userid, int theaterid, string title, DateTime date, string detail)
    {

        //string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
		int nT = 0;
		if (title != null)
			++nT;
		if (detail != null)
			++nT;
		nT += 3;
		string[] name = new string[nT];
		object[] value = new object[nT];
		name[0] =  "TheaterID" ;
		value[0] = theaterid;
		name[1] = "Date";
		value[1] = date;
		name[2] = "UserID";
		value[2] = userid;
		nT = 2;
		if (title != null)
		{
			name[++nT] = "Title";
			value[nT] = title;
		}
		if (detail != null)
		{
			name[++nT]="Detail";
			value[nT] = detail;
		}

		try
		{

			return DataBaseClass.insertData(name, value, "T_Comment", DataBaseClass.GetConnectionString());
		}
		catch (SqlException e)
		{
			System.Console.Write("TComment类：");
			System.Console.WriteLine(e.Message);
			return false;
		}
		catch (ArgumentException e)
		{
			System.Console.Write("TComment类：");
			System.Console.WriteLine(e.Message);
			return false;
		}
		catch (InvalidOperationException e)
		{
			System.Console.Write("TComment类：");
			System.Console.WriteLine(e.Message);
			return false;
		}
		catch (Exception e)
		{
			System.Console.Write("TComment类：");
			System.Console.WriteLine("未知错误:" + e.Message);
			return false;
		}
    }
    //2)保存自身到数据库
    public bool AddT_Comment()
    {
		bool bT =  TComment.AddT_Comment(m_iUserID , m_iTheaterID , m_sTitle , m_dtDate , m_sDetail);
		m_iID = DataBaseClass.GetMaxID("T_Comment" , DataBaseClass.GetConnectionString());
		return bT;
    }

    //3
    //void DelT_Comment()
    /// <summary>
    /// )删除此评论
    /// </summary>
    /// <param name="id">id</param>
    /// <returns>返回是否删除成功</returns>
    public static bool DelT_Comment(int id)
    {
		try
		{
			return DataBaseClass.deleteData("ID", id, "T_Comment", _connectionString);
		}
		catch (SqlException e)
		{
			System.Console.Write("TComment类：");
			System.Console.WriteLine(e.Message);
			return false;
		}
		catch (ArgumentException e)
		{
			System.Console.Write("TComment类：");
			System.Console.WriteLine(e.Message);
			return false;
		}
		catch (InvalidOperationException e)
		{
			System.Console.Write("TComment类：");
			System.Console.WriteLine(e.Message);
			return false;
		}
		catch (Exception e)
		{
			System.Console.Write("TComment类：");
			System.Console.WriteLine("未知错误:" + e.Message);
			return false;
		}
    }
	public bool DelT_Comment()
	{
		return DelT_Comment(this.IID);
	}
    //4)
    //Static DataTable<评论对象> GetT_Comment (,)
    /// <summary>
    /// 获得一个电影院的评论
    /// </summary>
    /// <param name="ID">电影院ID</param>
    /// <returns></returns>
    public static DataTable GetT_Comment(int ID)
    {
		//string sql = String.Empty;
        //if (type == "date")
        //{
        //        DateTime time = (DateTime)detail;
        //        DateTime now = DateTime.Now;
        //        sql = "SELECT * FROM T_Comment WHERE Date BETWEEN " + time.ToString() + " and " + now.ToString();
        //}
        //else if (type == "theater")
        //{
                //string movie = (string)detail;
				//sql = "SELECT * FROM T_Comment WHERE TheaterID = " + ID + ";";
       // }
       //else if (type == "user")
       // {
       //         string user = (string)detail;
       //         sql = "SELECT * FROM T_Comment WHERE UserID = '" + user + "';";
       //}


		//SqlDataAdapter dad = new SqlDataAdapter(sql, _connectionString);
		//DataTable dtblMCom = new DataTable();
		//dad.Fill(dtblMCom);
		try
		{
			return DataBaseClass.selectData("SELECT * FROM T_Comment WHERE TheaterID = " + ID, _connectionString);
		}
		catch (SqlException e)
		{
			System.Console.Write("TComment类：");
			System.Console.WriteLine(e.Message);
			return null;
		}
		catch (ArgumentException e)
		{
			System.Console.Write("TComment类：");
			System.Console.WriteLine(e.Message);
			return null;
		}
		catch (InvalidOperationException e)
		{
			System.Console.Write("TComment类：");
			System.Console.WriteLine(e.Message);
			return null;
		}
		catch (Exception e)
		{
			System.Console.Write("TComment类：");
			System.Console.WriteLine("未知错误:" + e.Message);
			return null;
		}
		//return dtblMCom;
    }


    static TComment()
    {
        _connectionString = DataBaseClass.GetConnectionString();
    }

    public static List<TComment> GetListFromDataTable(DataTable dt)
    {
        List<TComment> temp = new List<TComment>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            TComment tcomment = new TComment((int)dt.Rows[i]["ID"], (int)dt.Rows[i]["UserID"]
                , (int)dt.Rows[i]["TheaterID"], (DateTime)dt.Rows[i]["Date"]
                , dt.Rows[i]["Title"] as string, dt.Rows[i]["Detail"] as string);
            temp.Add(tcomment);
        }
        return temp;
    }
}
public class Test2
{
	public static void Test()
	{
		Theater.AddTheaterInfo(-1, "DSF", "ASDFASFasdf", "123afsd15", "5465adf43.com", 5.0M, "ASasdfDF", 12);
		Theater t = Theater.GetTheaterByName("DSF");
		TComment.AddT_Comment(1, t.ID, "adf", DateTime.Today, "32131");
		TComment comment = TComment.GetListFromDataTable(TComment.GetT_Comment(t.ID))[0];
		comment.DelT_Comment();
		int nT = TComment.GetListFromDataTable(TComment.GetT_Comment(t.ID)).Count;
		if (nT != 0)
			System.Console.WriteLine("单项删除有错");
		TComment.AddT_Comment(1, t.ID, "adf", DateTime.Today, "32131");
		nT = TComment.GetListFromDataTable(TComment.GetT_Comment(t.ID)).Count;
		if (nT == 0)
			System.Console.WriteLine("重复添加有错有错");
		t.DeleteSelfInfo();
		nT = TComment.GetListFromDataTable(TComment.GetT_Comment(t.ID)).Count;
		if (nT != 0)
			System.Console.WriteLine("联系删除有错");
		
		t.DeleteSelfInfo();
	}
}